package cn.cold.face;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestClientException;

public class CallFaceTask implements Runnable {
	private static final Logger log = LoggerFactory.getLogger(CallFaceTask.class);

	private File idfile;
	private File peoplefile;
	private String savePath;
	private LinkedList<File> idPath;
	private LinkedList<File> peoplePath;
	private Integer number;
	private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private TaskState task;
	private Integer i;
	private String address;
	public LinkedList<File> getIdPath() {
		return this.idPath;
	}

	public void setIdPath(LinkedList<File> idPath) {
		this.idPath = idPath;
	}

	public LinkedList<File> getPeoplePath() {
		return this.peoplePath;
	}

	public void setPeoplePath(LinkedList<File> peoplePath) {
		this.peoplePath = peoplePath;
	}

	public Integer getNumber() {
		return this.number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public String getSavePath() {
		return this.savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getIdfile() {
		return this.idfile;
	}

	public void setIdfile(File idfile) {
		this.idfile = idfile;
	}

	public File getPeoplefile() {
		return this.peoplefile;
	}

	public void setPeoplefile(File peoplefile) {
		this.peoplefile = peoplefile;
	}

	public TaskState getTask() {
		return this.task;
	}

	public void setTask(TaskState task) {
		this.task = task;
	}

	public static Logger getLog() {
		return log;
	}

	public static SimpleDateFormat getSdf() {
		return SDF;
	}

	public Integer getI() {
		return i;
	}

	public void setI(Integer i) {
		this.i = i;
	}

	public CallFaceTask(String id, String people, String save, Integer number1) {
		setIdPath(CheckFace.traverseFolder1(id));
		setPeoplePath(CheckFace.traverseFolder1(people));
		setSavePath(save);
		if (getIdPath() == null || getIdPath().isEmpty()) {
			log.warn("第一个相册读取不到照片，程序退出");
			return;
		}
		if (getPeoplePath() == null || getPeoplePath().isEmpty()) {
			log.warn("第二个相册读取不到照片,程序退出");
			return;
		}
		if ((getPeoplePath().size() > 0) && (getIdPath().size() > 0)) {
			this.number = number1;
			synchronized (this.number) {
				String idname = null;
				if (this.number.intValue() > getIdPath().size()) {
					this.number = Integer.valueOf(0);
				} else if (this.number.intValue() >= 0) {
					idname = ((File) this.idPath.get(this.number.intValue())).getName().trim().substring(0,
							((File) this.idPath.get(this.number.intValue())).getName().indexOf("-"));
					for (int i = 0; i < this.peoplePath.size(); i++) {
						String peopleName = ((File) this.peoplePath.get(i)).getName().trim().substring(0,
								((File) this.peoplePath.get(i)).getName().indexOf("-"));
						if (idname.equals(peopleName)) {
							setIdfile((File) this.idPath.get(this.number.intValue()));
							setPeoplefile((File) this.peoplePath.get(i));
							return;
						}
					}
				}
			}
		}
	}

	public void run() {
		long start = System.currentTimeMillis();
		this.task.AddTaskCount();
		SimpleIDSearchResponse response = null;
		try {
			response = MinshApi.OneToOne(this.idfile.getAbsolutePath(), this.peoplefile.getAbsolutePath(), address);
		} catch (RestClientException e) {
			this.task.setTimeoutCount(Integer.valueOf(1));
			e.printStackTrace();
		} catch (Exception e) {
			this.task.setErrorCount(Integer.valueOf(1));
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		this.task.setTime(end);
		this.task.setMaxTime(end);
		this.task.setMixTime(end);
		this.task.ReduceTaskCount();
		//log.info("本次比對結束, 開始時間為:{}, 結束時間為: {}, 縂耗時為: {}.", SDF.format(start), SDF.format(end), (end-start));
		CheckFace.method1(this.idfile.getAbsolutePath() + this.peoplefile.getAbsolutePath() + ","
				+ this.idfile.getName() + "," + this.peoplefile.getName() + "," + response.getPairVerifySimilarity()
				+ "," + (response.getPairVerifyResult() == 0 ? "YES" : "NO"), this.savePath);
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
}
